<?php

namespace Hdphim\Modules\Api\Models\Users;

use Phalcon\Mvc\Model\Validator\Uniqueness;
use Phalcon\Mvc\Model;

class UsersGroups extends Model
{
    /**
     *
     * @var integer
     */
    protected $id;

    /**
     *
     * @var string
     */
    protected $name;

    /**
     *
     * @var integer
     */
    protected $active;

    /**
     * Method to set the value of field id
     *
     * @param integer $id
     * @return $this
     */
    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }

    /**
     * Method to set the value of field name
     *
     * @param string $name
     * @return $this
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Method to set the value of field active
     *
     * @param integer $active
     * @return $this
     */
    public function setActive($active)
    {
        $this->active = $active;

        return $this;
    }

    /**
     * Returns the value of field id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Returns the value of field name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Returns the value of field active
     *
     * @return integer
     */
    public function getActive()
    {
        return $this->active;
    }

    public function getSource()
    {
        return 'users_groups';
    }

    public function validation()
    {
        $this->validate(new Uniqueness(array(
            'field' => 'name',
            'message'   => 'Group name already registered'
        )));

        return $this->validationHasFailed() != true;
    }

    public function initialize()
    {
        $this->hasMany('id' , 'Hdphim\Modules\Api\Users\Users' , 'group_id' , array(
            'alias' => 'user',
            'foreignKey' => array(
                'message' => 'Profile cannot be deleted because it\'s used on Users'
            )
        ));
        
        $this->hasMany('id' , 'Hdphim\Modules\Api\Models\Users\Permissions' , 'group_id' , array(
            'alias' => 'permissions'
        ));
    }
} 